//
//  LocalHeaderView.m
//  MiaoTu
//
//  Created by dllo on 16/9/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LocalHeaderView.h"
#import "UIView+Frame.h"
#import "Masonry.h"

@interface LocalHeaderView ()
@property (weak, nonatomic) IBOutlet UITextField *keywordTextField;
@property (weak, nonatomic) IBOutlet UILabel *nightLabel;
@property (weak, nonatomic) IBOutlet UIButton *searchButton;
@property (weak, nonatomic) IBOutlet UIButton *positionButton;
@property (weak, nonatomic) IBOutlet UILabel *startWeekdayLabel;
@property (weak, nonatomic) IBOutlet UILabel *endWeekdayLabel;
@property (weak, nonatomic) IBOutlet UILabel *startDayLabel;
@property (weak, nonatomic) IBOutlet UILabel *endDayLabel;

@end

@implementation LocalHeaderView

// 同时重写setter方法和getter方法需要加上这句话!!!
@synthesize startAndEndDate = _startAndEndDate;

- (void)setStartAndEndDate:(NSString *)startAndEndDate {
    _startAndEndDate = [startAndEndDate copy];
    
    NSArray *dateArray = [_startAndEndDate componentsSeparatedByString:@":"];
    _startDayLabel.text = [NSString stringWithFormat:@"%@>", dateArray.firstObject];
    _endDayLabel.text = [NSString stringWithFormat:@"%@>", dateArray.lastObject];
    
    NSDateFormatter *df = [NSDateFormatter new];
    [df setDateFormat:@"yyyy-MM-dd"];
    NSDate *startDate = [df dateFromString:dateArray.firstObject];
    NSDate *endDate = [df dateFromString:dateArray.lastObject];

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *startComps = [calendar components:NSCalendarUnitWeekday fromDate:startDate];
    NSDateComponents *endComps = [calendar components:NSCalendarUnitWeekday fromDate:endDate];
    NSArray *weekdayArray = @[@"0", @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六"];
    _startWeekdayLabel.text = weekdayArray[startComps.weekday];
    _endWeekdayLabel.text = weekdayArray[endComps.weekday];
    
    NSDateComponents *comps = [calendar components:NSCalendarUnitDay fromDate:startDate toDate:endDate options:NSCalendarWrapComponents];
    _nightLabel.text = [NSString stringWithFormat:@"%ld晚", (long)comps.day];
}

- (NSString *)startAndEndDate {
    return [NSString stringWithFormat:@"%@ %@", [_startDayLabel.text stringByReplacingOccurrencesOfString:@">" withString:@""], [_endDayLabel.text stringByReplacingOccurrencesOfString:@">" withString:@""]];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    [_keywordTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_keywordTextField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    _nightLabel.layer.borderWidth = 2;
    _nightLabel.layer.borderColor = [UIColor whiteColor].CGColor;
    
    CGFloat searchImageH = _searchButton.titleLabel.dk_height;
    CGFloat searchImageT = (_searchButton.dk_height - searchImageH) / 2;
    CGFloat searchImageL = _searchButton.titleLabel.dk_x - searchImageH;
    CGFloat searchImageR = _searchButton.dk_width - _searchButton.titleLabel.dk_x;
    _searchButton.imageEdgeInsets = UIEdgeInsetsMake(searchImageT, searchImageL, searchImageT, searchImageR);

    CGFloat positionImageT = (_positionButton.dk_height - _positionButton.titleLabel.dk_height) / 2;
    CGFloat positionImageL = (_positionButton.dk_width - _positionButton.titleLabel.dk_width - _positionButton.titleLabel.dk_height) / 2;
    CGFloat positionImageR = positionImageL + _positionButton.titleLabel.dk_width;
    _positionButton.imageEdgeInsets = UIEdgeInsetsMake(positionImageT, positionImageL, positionImageT, positionImageR);
    _positionButton.titleEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
}

+ (instancetype)headerView {
    NSArray *nibView = [[NSBundle mainBundle] loadNibNamed:@"LocalHeaderView" owner:self options:nil];
    return [nibView objectAtIndex:0];
}

@end
